import mongoose from "mongoose";
import config from "@/config/config.default";

const connectToDatabase = async () => {
  const { baseUri, host, port, database, options } = config.mongooseOptions;
  // const { baseUri, username, password, host, port, database, options } =
  //   config.mongooseOptions;
  // const MONGODB_URI = `${baseUri}${username}:${password}@${host}:${port}/${database}`;
  const MONGODB_URI = `${baseUri}${host}:${port}/${database}`;
  console.log("数据库连接地址：", MONGODB_URI);
  try {
    await mongoose.connect(MONGODB_URI, options);
    console.log("Connected to MongoDB");
  } catch (error) {
    console.error(error);
    process.exit(1);
  }
};
export default connectToDatabase;
